package com.psychologyCommunity.Community.quartz;

import com.psychologyCommunity.Community.Contoller.UserController;
import com.psychologyCommunity.Community.entity.DiscussPost;
import com.psychologyCommunity.Community.service.DiscussPostService;
import com.psychologyCommunity.Community.service.ElasticsearchService;
import com.psychologyCommunity.Community.service.LikeService;
import com.psychologyCommunity.Community.util.CommunityConstance;

import com.psychologyCommunity.Community.util.RedisKeyUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


public class PostScoreReflashJob implements Job, CommunityConstance {
    //注：做定时任务 时，在关键的节点 打印一下日志，这样意外中断之后好友一个依据
    private static final Logger logger= LoggerFactory.getLogger(PostScoreReflashJob.class);

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private LikeService likeService;

    @Autowired
    private ElasticsearchService elasticsearchService;

    //要初始化一个常量数据 ， 心理健康网纪元--成立的日志
    private static  Date epoch;
    static { try {
            epoch=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-05-20 00:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
        } }
    //查帖子 查统计的数据  并记得到时候更新到ES中
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        String postScoreKey = RedisKeyUtil.getPostScoreKey();
        BoundSetOperations operations = redisTemplate.boundSetOps(postScoreKey);
        //如果没有人进行操作和计算 此时这个任务就让他取消、
        if(operations.size() ==0){
            logger.info("任务取消，没有要更新的帖子！！！！");
            return;
        }else {
            logger.info("任务开始，正在刷新帖子分数~~~~");
            //只要redis中有数据 就去算
            while (operations.size()>0){
                this.refresh((Integer) operations.pop()); }
            logger.info("任务结束，帖子刷新完毕！！！");
        } }
    private void refresh(int postId){
        DiscussPost discussPost = discussPostService.findDiscussPostById(postId);
        if(discussPost==null){
            logger.error("该帖子不存在，id是"+postId);
            return ; }
        boolean  wonderful= discussPost.getStatus()==1;//是否是精华帖子
        int commentCount = discussPost.getCommentCount(); //评论数量
        long LikeCount=likeService.findEntityLikeCount(ENTITY_TYPE_POST,postId);//点赞数量
        //计算权重
        double  w = (wonderful ? 75 : 0) + commentCount * 10 + LikeCount * 2;
        //分数=帖子权重 +距离天数
        double score = Math.log10(Math.max(w, 1))
                + (discussPost.getCreateTime().getTime() - epoch.getTime()) / (1000 * 3600 * 24);
        //更新帖子分数
        discussPostService.updateScore(postId,score);
        //记得同步搜索的数据--暂没完成 ，有问题
//        elasticsearchService.save

    }
}
